package com.example.mycomponent.component.wheelpicker

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

val HOURS: List<String> = listOf(
    "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11",
    "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"
)
val MINUTES: List<String> = listOf(
    "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
    "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
    "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
    "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"
)

@Composable
fun TimeWheelPicker(
    timeWheelPickerState: TimeWheelPickerState,
    modifier: Modifier = Modifier,
    width: Dp = 100.dp,
    itemHeight: Dp = 40.dp,
    numberOfDisplayedItems: Int = 5,
    itemScaleFact: Float = 1.5f,
    textStyle: TextStyle = MaterialTheme.typography.bodyLarge,
    textColor: Color = Color.Gray,
    selectedTextColor: Color = MaterialTheme.colorScheme.primary
) {
    Row(
        modifier = modifier.fillMaxWidth(),
    ) {
        Spacer(modifier = Modifier.weight(1f))
        WheelPicker(
            width = width,
            itemHeight = itemHeight,
            numberOfDisplayedItems = numberOfDisplayedItems,
            items = HOURS,
            initialItem = timeWheelPickerState.initialHour.toTimeString(),
            itemScaleFact = itemScaleFact,
            textStyle = textStyle,
            textColor = textColor,
            selectedTextColor = selectedTextColor,
            onItemSelected = { _, item ->
                timeWheelPickerState.initialHour = item.toInt()
            },
            modifier = Modifier.weight(1f)
        )
        Box(
            modifier = Modifier
                .weight(1f)
                .height(itemHeight * numberOfDisplayedItems),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = ":",
                textAlign = TextAlign.Center,
                style = MaterialTheme.typography.bodyLarge,
                color = MaterialTheme.colorScheme.primary,
                fontWeight = FontWeight.Bold,
                fontSize = MaterialTheme.typography.bodyLarge.fontSize * 1.5f
            )
        }
        WheelPicker(
            width = width,
            itemHeight = itemHeight,
            numberOfDisplayedItems = numberOfDisplayedItems,
            items = MINUTES,
            initialItem = timeWheelPickerState.initialMinute.toTimeString(),
            itemScaleFact = itemScaleFact,
            textStyle = textStyle,
            textColor = textColor,
            selectedTextColor = selectedTextColor,
            onItemSelected = { _, item ->
                timeWheelPickerState.initialMinute = item.toInt()
            },
            modifier = Modifier.weight(1f)
        )
        Spacer(modifier = Modifier.weight(1f))
    }
}

private fun Int.toTimeString(): String {
    return if(this < 10) "0$this" else this.toString()
}

@Preview(showBackground = true)
@Composable
private fun TimeWheelPickerPreview() {
    TimeWheelPicker(
        timeWheelPickerState = rememberTimeWheelPickerState(),
        modifier = Modifier.fillMaxSize()
    )
}